package com.ruoyi.service.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.archive.domain.ArchiveBuilding;
import com.ruoyi.archive.domain.ArchivePerson;
import com.ruoyi.archive.domain.ArchiveRoom;
import com.ruoyi.archive.mapper.ArchiveBuildingMapper;
import com.ruoyi.archive.mapper.ArchiveRoomMapper;
import com.ruoyi.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.validation.Validator;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.service.mapper.ServiceCheckMeterMapper;
import com.ruoyi.service.domain.ServiceCheckMeter;
import com.ruoyi.service.service.IServiceCheckMeterService;

/**
 * 抄记录Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-04-21
 */
@Service
public class ServiceCheckMeterServiceImpl implements IServiceCheckMeterService 
{
    private static final Logger log = LoggerFactory.getLogger(ServiceCheckMeterServiceImpl.class);

    @Autowired
    private ServiceCheckMeterMapper serviceCheckMeterMapper;
    @Autowired
    private ArchiveBuildingMapper archiveBuildingMapper;
    @Autowired
    private ArchiveRoomMapper archiveRoomMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询抄记录
     * 
     * @param id 抄记录主键
     * @return 抄记录
     */
    @Override
    public ServiceCheckMeter selectServiceCheckMeterById(Long id)
    {
        return serviceCheckMeterMapper.selectServiceCheckMeterById(id);
    }

    /**
     * 查询抄记录列表
     * 
     * @param serviceCheckMeter 抄记录
     * @return 抄记录
     */
    @Override
    public List<ServiceCheckMeter> selectServiceCheckMeterList(ServiceCheckMeter serviceCheckMeter)
    {
        return serviceCheckMeterMapper.selectServiceCheckMeterList(serviceCheckMeter);
    }

    /**
     * 新增抄记录
     * 
     * @param serviceCheckMeter 抄记录
     * @return 结果
     */
    @Override
    public int insertServiceCheckMeter(ServiceCheckMeter serviceCheckMeter)
    {
        if(serviceCheckMeter.getDeptId() == null){
            ArchiveRoom archiveRoom = archiveRoomMapper.selectArchiveRoomById(serviceCheckMeter.getRoomId());
            serviceCheckMeter.setDeptId(archiveRoom.getDeptId());
        }
        serviceCheckMeter.setCreateTime(DateUtils.getNowDate());
        return serviceCheckMeterMapper.insertServiceCheckMeter(serviceCheckMeter);
    }

    /**
     * 修改抄记录
     * 
     * @param serviceCheckMeter 抄记录
     * @return 结果
     */
    @Override
    public int updateServiceCheckMeter(ServiceCheckMeter serviceCheckMeter)
    {
        serviceCheckMeter.setUpdateTime(DateUtils.getNowDate());
        return serviceCheckMeterMapper.updateServiceCheckMeter(serviceCheckMeter);
    }

    /**
     * 批量删除抄记录
     * 
     * @param ids 需要删除的抄记录主键
     * @return 结果
     */
    @Override
    public int deleteServiceCheckMeterByIds(Long[] ids)
    {
        return serviceCheckMeterMapper.deleteServiceCheckMeterByIds(ids);
    }

    /**
     * 删除抄记录信息
     * 
     * @param id 抄记录主键
     * @return 结果
     */
    @Override
    public int deleteServiceCheckMeterById(Long id)
    {
        return serviceCheckMeterMapper.deleteServiceCheckMeterById(id);
    }


    /**
   * 导入数据
   *
   * @param dataList 数据列表
   * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
   * @param operName 操作用户
   * @return 结果
   */
    @Override
    public String importData(List<ServiceCheckMeter> dataList, Boolean isUpdateSupport, String operName){
        if (StringUtils.isNull(dataList) || dataList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        Map<String, ArchiveBuilding> buildingMap  = new HashMap<>();
        List<ArchiveBuilding> buildingList = archiveBuildingMapper.selectArchiveBuildingList(new ArchiveBuilding());
        if(buildingList == null || buildingList.isEmpty()){
            throw new ServiceException("请先添加楼栋信息");
        }
        for(ArchiveBuilding building:buildingList){
            buildingMap.put(building.getDeptName()+"_"+building.getName(), building);
        }
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ServiceCheckMeter row : dataList)
        {
            try
            {
                ArchiveBuilding archiveBuilding = buildingMap.get(row.getDeptName()+"_"+row.getBuildingName());
                if(archiveBuilding == null){
                    throw new ServiceException("楼栋不存在:"+row.getDeptName() + " " + row.getBuildingName());
                }
                row.setDeptId(archiveBuilding.getDeptId());

                ArchiveRoom archiveRoom = archiveRoomMapper.selectArchiveRoomByName(archiveBuilding.getId(), row.getUnitNum(), row.getRoomName());
                if(archiveRoom == null){
                    throw new ServiceException("房屋不存在:"+row.getDeptName() + " " + row.getBuildingName() + " " + row.getUnitNum() + " " + row.getRoomName());
                }
                row.setRoomId(archiveRoom.getId());

                BeanValidators.validateWithException(validator, row);
                insertServiceCheckMeter(row);

                successMsg.append("<br/>" + successNum + "、" + row.getDeptName() + " " + row.getBuildingName() + " " + row.getUnitNum() + " " + row.getRoomName() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、" + row.getDeptName() + " " + row.getBuildingName() + " " + row.getUnitNum() + " " + row.getRoomName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
